package rs.networx.servlets;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.PersonDao;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class EditPersonalInformationServlet extends HttpServlet {

	private static final long serialVersionUID = -8492365754611574708L;

	@EJB
	private PersonDao personDao;

	private String username;
	private String password;
	private Person loggedUser;

	private String name;
	private String surname;
	private String dateOfBirth;
	private String sex;
	private String city;
	private String education;
	private String employment;
	private String webAdress;
	
	//private String cleanedUpDateOfBirth;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("Loaded: EDIT PERSONAL INFORMATION");

		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());

		name = request.getParameter("PersonalProfileName");
		surname = request.getParameter("PersonalProfileSurname");
		dateOfBirth = request.getParameter("PersonalProfileDateOfBirth");
		sex = request.getParameter("PersonalProfileSex");
		city = request.getParameter("PersonalProfileCity");
		education = request.getParameter("PersonalProfileEducation");
		employment = request.getParameter("PersonalProfileEmployment");
		webAdress = request.getParameter("PersonalProfileWebAdress");

		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");

		username = loggedUser.getEmail();
		password = loggedUser.getPassword();
		System.out.println(username + "  " + password);

		System.out.println("username: " + username + "/n" +
				"password: " + password + "/n" +
				"name: " + name +"/n" +
				"surname: " + surname +"/n" + 
				"dateOfBirth: " + dateOfBirth +"/n" +
				"sex: " + sex +"/n" +
				"city: " + city +"/n" +
				"education: " + education +"/n" +
				"employment: " + employment +"/n" +
				"webAdress: " + webAdress);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		
		loggedUser.setFirstName(name);
		loggedUser.setLastName(surname);
		
		try {
			loggedUser.setDob(simpleDateFormat.parse(dateOfBirth));
		} catch (Exception e) {
			//e.printStackTrace();
			
			// dodati obradu greske ako dateOfBirth nije unesen u ispravnom formatu...
			request.setAttribute("dateStatus", ErrorMessages.INVALID_DATE_FORMAT);
			
			request.getRequestDispatcher("ViewSelectedPostsPage.jsp").forward(request, response);
			return;
		}
		
		loggedUser.setSex(sex);
		loggedUser.setPlace(city);
		loggedUser.setWebSite(webAdress);
		loggedUser.setEducation(education);
		loggedUser.setEmployment(employment);
		
		loggedUser.setEmail(username);
		loggedUser.setPassword(password);
		
		loggedUser.setActivated(true);
		
		// update podataka u bazi...
		personDao.merge(loggedUser);
		
		// i vracamo se na home page logovanog korisnika...
		response.sendRedirect("LoginServlet");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}

}
